/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport.Reflection;
import org.openide.nodes.Sheet;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.util.lookup.Lookups;
import pt.utl.ist.isr.livetrack.server.LTServer;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class ServerNode extends AbstractNode {

    private static Action[] actions = new Action[]{
        new org.openide.util.actions.SystemAction() {

            @Override
            public String getName() {
                return NbBundle.getMessage(this.getClass(), "DeleteServer");
            }

            @Override
            public HelpCtx getHelpCtx() {
                return null;
            }

            @Override
            public void actionPerformed(ActionEvent ae) {
                Settings.loadSettings().getServers().remove(Utilities.actionsGlobalContext().lookup(ServerNode.class).settingsServer);
                Settings.loadSettings().save();
                ServersNodeChildFactory.getInstance().refresh();
            }
        },
        new org.openide.util.actions.SystemAction() {

            @Override
            public String getName() {
                return NbBundle.getMessage(this.getClass(), "SaveServerState");
            }

            @Override
            public HelpCtx getHelpCtx() {
                return null;
            }

            @Override
            public void actionPerformed(ActionEvent ae) {
                Utilities.actionsGlobalContext().lookup(ServerNode.class).server.saveToFile();
            }
        }};
    private Settings.Server settingsServer;
    private LTServer server;
    private Sheet sheet;
    private Sheet.Set set;

    public ServerNode(Settings.Server settingsServer, LTServer server) {
        super(Children.create(new ServerNodeChildFactory(server), false), Lookups.fixed(settingsServer, server));
        this.server = server;
        this.settingsServer = settingsServer;
        setDisplayName("" + settingsServer);
        init();
    }

    public Image getOpenedIcon(int type) {
        return getIcon(type);
    }

    public Image getIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/server.jpg", true);
    }

    private void init() {

        sheet = Sheet.createDefault();
        set = Sheet.createPropertiesSet();
        final LTServer obj = getLookup().lookup(LTServer.class);

//        try {
//            addProperties(new PropertySupport.Reflection[]{},
//                    new String[]{});
//
//
//        } catch (NoSuchMethodException ex) {
//            ErrorManager.getDefault();
//        }

        sheet.put(set);
    }

    private void addProperties(Reflection[] props, String[] names) {
        for (int i = 0; i < props.length; i++) {
            Property property = props[i];
            property.setName(names[i]);
            set.put(property);
        }
    }

    @Override
    protected Sheet createSheet() {
        return sheet;
    }

    @Override
    public Action[] getActions(boolean context) {
        return actions;
    }
}
